package com.dd.ent.car.common.util;

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 获取屏幕大小等工具类
 * <p/>
 * Created by lixianpeng on 2016/9/12.
 * Copyright (c) 2016 Xunlei. All rights reserved.
 *
 * @author lixianpeng
 * @since 2016/9/12
 */
@SuppressWarnings("unused")
public abstract class ScreenUtil {

    /**
     * 获取屏幕的宽度(单位像素)
     */
    public static int getScreenWidth(Context context) {
        if (context != null) {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(dm);
            return dm.widthPixels;
        }
        return 0;
    }

    /**
     * 获取屏幕的高度(单位像素)
     */
    public static int getScreenHeight(Context context) {
        if (context != null) {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(dm);
            return dm.heightPixels;
        }
        return 0;
    }

    /**
     * 获取状态栏高度(单位像素)
     *
     * @author zengnianan
     */
    public static int getStatusBarHeight(Context context) {
        int height = 0;
        try {
            Class c = Class.forName("com.android.internal.R$dimen");

            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int resId = Integer.parseInt(field.get(obj).toString());
            height = context.getResources().getDimensionPixelSize(resId);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return height;
    }

    /**
     * 获取导航栏高度(单位像素)
     *
     * @author zengnianan
     **/
    public static int getNavigationBarHeight(Context context) {
        int navigationBarHeight = 0;

        //是否有虚拟导航栏
        int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid == 0) {
            return navigationBarHeight;
        }

        boolean showNavigationBar = context.getResources().getBoolean(rid);
        if (!showNavigationBar) {
            return navigationBarHeight;
        }
        int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        navigationBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        return navigationBarHeight;
    }

    /**
     * 获取屏幕的真正的宽高尺寸(单位像素)
     *
     * @author zengnianan
     */
    public static Point getScreenRealSizeInPixels(Context context) {
        Point size = new Point(0, 0);
        if (context == null) {
            return size;
        }
        final DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        try {
            // For JellyBean 4.2 (API 17) and onward
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
                display.getRealMetrics(metrics);
                size.x = metrics.widthPixels;
                size.y = metrics.heightPixels;
            } else {
                Method mGetRawH = null, mGetRawW = null;
                mGetRawH = Display.class.getMethod("getRawHeight");
                mGetRawW = Display.class.getMethod("getRawWidth");

                try {
                    size.x = (Integer) mGetRawW.invoke(display);
                    size.y = (Integer) mGetRawH.invoke(display);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (NoSuchMethodException e3) {
            e3.printStackTrace();
        }

        return size;
    }

    /**
     * 获取屏幕密度
     */
    public static float getScreenDensity(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metric);
        return metric.density;
    }
}
